#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/netlink.h>
#include <errno.h>
#include <unistd.h>
#include <regex.h>


#define UEVENT_BUFFER_SIZE 128

static int init_hotplug_sock(void)
{
    struct sockaddr_nl snl;
    const int buffersize = 16 * 1024 * 1024;
    int retval;
    memset(&snl, 0x00, sizeof(struct sockaddr_nl));
    snl.nl_family = AF_NETLINK;
    snl.nl_pid = getpid();
    snl.nl_groups = 1;
    int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if (hotplug_sock == -1)
    {
        printf("error getting socket: %s", strerror(errno));
        return -1;
    }
    
    /* set receive buffersize */
    setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));

    retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
    if (retval < 0) {
        printf("bind failed: %s", strerror(errno));
        close(hotplug_sock);
        hotplug_sock = -1;
        return -1;
    }
    return hotplug_sock;
}

/* 取子串的函数 */
static char* substr(const char*str, unsigned start, unsigned end)
{
    unsigned n = end - start;
    static char stbuf[256];
    strncpy(stbuf, str + start, n);
    stbuf[n] = 0;
    return stbuf;
}


int main(int argc, char* argv[])
{
         int hotplug_sock  = init_hotplug_sock();
	 int x, z, lno = 0;
/*****

REG_EXTENDED 以功能更加强大的扩展正则表达式的方式进行匹配。
REG_ICASE 匹配字母时忽略大小写。
REG_NOSUB 不用存储匹配后的结果。
REG_NEWLINE 识别换行符，这样’$’就可以从行尾开始匹配，’^’就可以从行的开头开始匹配。

*****/
	 int cflags = REG_EXTENDED | REG_NEWLINE; // 默认的0 不支持 扩展的正则表达式
	 regex_t  reg;
         regmatch_t  pm[10];
	 const  size_t  nmatch = 10;

//	 char *pattern="^move@.*/usb1/.*net/ww(\\w+)";
//	 char *pattern="^move@.*/usb1/.*net/.*";
//	 char *pattern=".*net.*";
//	 char *pattern="(\\w+)@.*?/usb1/";
//	 char *pattern="^move@.*?/usb1/.*net.*(\w+)";
	 char *pattern="^move@.*?/usb1/.*net/(\\w+)";
//	 char *pattern="^move@.*/usb1/.*net.*[0-9a-zA-Z]+";
//	 char *pattern="^move@.*?/.*(\w+)";

	 char ebuf[128], lbuf[128];

         while(1)
         {
               char buf[UEVENT_BUFFER_SIZE*2] = {0};
               recv(hotplug_sock, &buf, sizeof(buf), 0);
	       lno++;

//	      printf("%s\n",buf);
//	      printf("[%s]\n",buf);

	       z = regcomp(&reg, pattern, cflags);

   		if (z != 0){
      			regerror(z, &reg, ebuf, sizeof(ebuf));
      			fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern);
      			return 1;
    		}
		
	 	z = regexec(&reg, buf, nmatch, pm, 0);
		
		if (z == REG_NOMATCH)
		{
//	      		printf("[%s] continue\n",buf);		
              		continue;
		}else if (z != 0) {
        			regerror(z, &reg, ebuf, sizeof(ebuf));
				printf("[%s] 1\n",buf);
        			fprintf(stderr, "%s: regcom('%s')\n", ebuf, buf);
        			return 2;
		}

		printf("[%s] 2\n",buf);

		for(x=0; x < nmatch && pm[x].rm_so != -1; x++)
		{
			if(!x){
				printf("%04d: %s\n", lno, buf);
			}
			printf(" $%d='%s'\n", x, substr(buf, pm[x].rm_so, pm[x].rm_eo));
		}
      }

	regfree(&reg);

         return 0;
}

/************

[move@/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.4/net/wwp0s20u2i4] 2
0057: move@/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.4/net/wwp0s20u2i4
 $0='move@/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.4/net/wwp0s20u2i4'
 $1='wwp0s20u2i4'


********/
